<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_type_dbc.class.php');


class Menu_Manage_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_menu/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$manage_event = new Web_Event_Simple_Int('manage', Tangra_Parameter_Method::GET, 'manage');
		$this->wed->add_wed_event_action_pair($manage_event, 'manage_event', 50);

		$move_up_event = new Web_Event_Simple_Int('move_up', Tangra_Parameter_Method::GET, 'move_up');
		$move_up_event_comp = new Web_Event_Composite('move_up_event_comp');
		$move_up_event_comp->add_wes($move_up_event);
		$move_up_event_comp->add_wes($manage_event);
		$this->wed->add_wed_event_action_pair($move_up_event_comp, 'move_up_event', 10);

		$move_down_event = new Web_Event_Simple_Int('move_down', Tangra_Parameter_Method::GET, 'move_down');
		$move_down_event_comp = new Web_Event_Composite('move_down_event_comp');
		$move_down_event_comp->add_wes($move_down_event);
		$move_down_event_comp->add_wes($manage_event);
		$this->wed->add_wed_event_action_pair($move_down_event_comp, 'move_down_event', 20);

		$publish_event = new Web_Event_Simple_Int('publish', Tangra_Parameter_Method::GET, 'publish');
		$publish_event_comp = new Web_Event_Composite('publish_event_comp');
		$publish_event_comp->add_wes($publish_event);
		$publish_event_comp->add_wes($manage_event);
		$this->wed->add_wed_event_action_pair($publish_event_comp, 'publish_event', 30);

		$unpublish_event = new Web_Event_Simple_Int('unpublish', Tangra_Parameter_Method::GET, 'unpublish');
		$unpublish_event_comp = new Web_Event_Composite('unpublish_event_comp');
		$unpublish_event_comp->add_wes($unpublish_event);
		$unpublish_event_comp->add_wes($manage_event);
		$this->wed->add_wed_event_action_pair($unpublish_event_comp, 'unpublish_event', 40);
	}


	protected function manage_event($menu_id) {
		$dbc = $this->get_dbc();
		$menu = new TCMS_Menu_DBC();
		if ($menu->load_by_id($dbc, $menu_id)) {
			$this->export('tcms_menu', $menu);

			$menu_type = new TCMS_Menu_Type_DBC();
			if (!$menu_type->load_by_id($dbc, $menu->get_type())) {
				throw new Tangra_Modules_Exception('Cannot load TCMS_Menu_Type_DBC with id = '.$menu->get_type());
			}
			$this->export('menu_type_name', $menu_type->get_name());

			$menu_items = TCMS_Menu_Item_DBC::load_menu_items_full($dbc, $menu_id, $this->_config->get_hidden_dir().'inc/');
			$this->export('menu_items', $menu_items);


			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "manage" parameter.');
	}


	protected function move_up_event($params) {

		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			TCMS_Menu_Item_DBC::move_up($dbc, $params['move_up'], $params['manage']);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->manage_event($params['manage']);
		return $view;
	}


	protected function move_down_event($params) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			TCMS_Menu_Item_DBC::move_down($dbc, $params['move_down'], $params['manage']);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->manage_event($params['manage']);
		return $view;
	}

	protected function publish_event($params) {
		$dbc = $this->get_dbc();

		$menu_item = new TCMS_Menu_Item_DBC();
		if ($menu_item->load_by_id($dbc, $params['publish'])) {
			$type = new TCMS_Menu_Item_Type_DBC();
			if (!$type->load_by_id($dbc, $menu_item->get_type())) {
				throw new Tangra_Modules_Exception('Cannot load TCMS_Menu_Item_Type_DBC with id = '.$menu_item->get_type());
			}

			$item_spec = $this->load_specific_item($dbc, $params['publish'], $type, $this->_config->get_hidden_dir().'inc/');
			if ($item_spec->can_publish()) {
				TCMS_Menu_Item_DBC::publish($dbc, $params['publish']);
			}
		}


		$view = $this->manage_event($params['manage']);
		return $view;
	}


	protected function unpublish_event($params) {
		$dbc = $this->get_dbc();

		TCMS_Menu_Item_DBC::unpublish($dbc, $params['unpublish']);


		$view = $this->manage_event($params['manage']);
		return $view;
	}


	private function load_specific_item(DB_Connection $dbc, $item_id, TCMS_Menu_Item_Type_DBC $type, $hidden_inc_dir) {
		$ret = '';

		$class_name = $type->get_class_name();

		if (!class_exists($class_name)) {
			require_once($hidden_inc_dir.$type->get_class_path());
		}

		$tmp = new $class_name;
		$tmp->load_by_id($dbc, $item_id);

		return $tmp;
	}
}

